package com.oazon.moco.link.ui;

import io.netty.channel.ChannelHandlerContext;

import java.util.Arrays;
import java.util.List;

import android.util.Log;

import com.oazon.moco.bean.McServiceMsg;
import com.oazon.moco.lan.core.controldriver.bean.ControlDriver;
import com.oazon.moco.link.core.LinkEvent;
import com.oazon.moco.link.core.LinkEventListener;
import com.oazon.moco.link.core.source.LanEventSource;
import com.oazon.moco.link.core.source.LinkEventSource;
import com.oazon.moco.link.event.LanEvent;
import com.oazon.moco.link.event.LanStatus;

public class LanEventListener extends LinkEventListener{

	private static final String TAG = "LanEventListener";
	
	public LanEventListener(LinkEventSource... sources) {
		setBlackList(Arrays.asList(sources));
	}

	@Override
	public void onLinkEvent(LinkEvent event) {
		
		if(filter((LinkEventSource)event.getSource()) || !(event instanceof LanEvent)){
			return;
		}
		
		LanEvent lanEvent = (LanEvent) event;
		
		if(lanEvent.getLanStatus() == LanStatus.SUBSCRIBE){
			subscribe(lanEvent.getControlDriver());
		}else
		
		if(lanEvent.getLanStatus() == LanStatus.UNSUBSCRIBE){
			unsubscribe(lanEvent.getControlDriver());
		}else
		if(lanEvent.getLanStatus() == LanStatus.OPEN_SERVICE){
			openService(lanEvent.getPort());
		}else
		if(lanEvent.getLanStatus() == LanStatus.OPEN_SERVICE_CALLBACK){
			openServiceCallback(lanEvent.isOpenServiceCallback());
		}else
		if(lanEvent.getLanStatus() == LanStatus.CLOSE_SERVICE){
			closeService();
		}else
		if(lanEvent.getLanStatus() == LanStatus.CLOSE_SERVCE_CALLBACK){
			closeServiceCallback(lanEvent.isCloseServiceCallback());
		}else
		if(lanEvent.getLanStatus() == LanStatus.WIFI_CONNECTED){
			wifiConnectedChange(lanEvent.isWificonnected(),	lanEvent.getWifiName());
		}else
		if(lanEvent.getLanStatus() == LanStatus.SERVICE_ADDED){
			serviceAdded(lanEvent.getMcServiceMsg(),((LanEventSource)lanEvent.getSource()).getUuid());
		}else if(lanEvent.getLanStatus() == LanStatus.SERVICE_DELETE){
			LanEventSource lanSource = (LanEventSource)lanEvent.getSource();
			serviceDelete(lanEvent.getServiceName(),lanSource.getUuid(),lanSource.getChannelContext());
		}else if(lanEvent.getLanStatus() == LanStatus.SERVICE_NOTIFY){
			notifyServiceChange(lanEvent.getMcServiceMsgs(),lanEvent.getUuid());
		}
		
	}
	
	protected void notifyServiceChange(List<McServiceMsg> list, String uuid){
		
	}
	
	protected void serviceDelete(String serviceName, String uuid, ChannelHandlerContext channelHandlerContext) {
		
	}

	protected void wifiConnectedChange(boolean wificonnected, String wifiName) {
		
	}

	/**
	 * 关闭服务成功或失败
	 * @param closeServiceCallback
	 */
	protected void closeServiceCallback(boolean closeServiceCallback) {
		
	}

	/**
	 * 关闭服务
	 * 		lan端实现
	 */
	protected void closeService() {
		
	}

	/**
	 * 打开服务成功或失败
	 * @param openServiceCallback
	 */
	protected void openServiceCallback(boolean openServiceCallback) {
		
	}

	/**
	 * 打开服务
	 * 		lan端实现
	 */
	protected void openService(int port) {
		
	}

	/**
	 * 当有控制设备订阅的时候
	 * @param controlDriver
	 */
	public void subscribe(ControlDriver controlDriver){
		Log.i(TAG, "subscribe  ip:"+controlDriver.getIp()+":"+controlDriver.getPort()+"||uuid:"+controlDriver.getUuid());
	}
	
	/**
	 * 当控制设备取消订阅的时候
	 * @param controlDriver
	 */
	public void unsubscribe(ControlDriver controlDriver){
		Log.i(TAG, "unsubscribe  ip:"+controlDriver.getIp()+":"+controlDriver.getPort()+"||uuid:"+controlDriver.getUuid());
	}

	public void serviceAdded(McServiceMsg msg,String uuid) {

	}
}
